﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern
{
    public partial class Contacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential("duytruong.ctk33@gmail.com", "truong@1234");
            SmtpServer.Port = 25;
            SmtpServer.Host = "smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();

            try
            {
                mail.From = new MailAddress(txtEmail.Text, txtHoTen.Text + " gửi từ form liên hệ", System.Text.Encoding.UTF8);
                mail.To.Add("duytruong.ctk33@gmail.com");
                mail.To.Add("huuquang91@gmail.com");
                mail.Subject = "Email liên hệ";
                mail.Body = MailBody();
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress(txtEmail.Text);
                mail.Priority = MailPriority.High;
                mail.IsBodyHtml = true;
                SmtpServer.Send(mail);
                Label1.Text = "Thanhk for you!";
                ResetFrom();
            }
            catch (Exception ex) { Label1.Text = ex.Message.ToString(); }
        }

        private void ResetFrom()
        {
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtNoiDung.Text = "";
        }

        private string MailBody()
        {
            string strHTML = "";
            strHTML += "Họ và tên: " + txtHoTen.Text + "<br>";
            strHTML += "Địa chỉ: " + txtDiaChi.Text + "<br>";
            strHTML += "Điện thoại: " + txtDienThoai.Text + "<br>";
            strHTML += "Email: " + txtEmail.Text + "<br>";
            strHTML += "Đã gửi qua Form liên hệ với thông điệp:<br><b>";
            strHTML += txtNoiDung.Text + "</b>";
            return strHTML;
        }
    }
}